package org.yys.seata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.yys.seata.entity.AccountEntity;
import org.yys.seata.service.AccountService;
import org.yys.seata.mapper.AccountMapper;
import org.springframework.stereotype.Service;

/**
* @author A
* @description 针对表【t_account】的数据库操作Service实现
* @createDate 2023-12-05 15:29:12
*/
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountEntity>
    implements AccountService{

    @Override
    public void decrease(Long userId, Integer money) {
        LambdaQueryWrapper<AccountEntity> eq = Wrappers.<AccountEntity>lambdaQuery().eq(AccountEntity::getUserId, userId);
        AccountEntity one = this.getOne(eq);

        if (one==null) {
            throw new RuntimeException("账户为空");
        }


        one.setResidue(one.getResidue()-money);
        one.setUsed(one.getUsed()+money);

        updateById(one);
    }
}




